package com.aspsine.multithreaddownload;

import com.aspsine.multithreaddownload.db.DataBaseManager;
import com.aspsine.multithreaddownload.db.ThreadInfo;
import ohos.app.Context;
import org.junit.Assert;
import org.junit.Test;

import java.util.List;

public class DatabaseOhosTest {

    Context  app = App.getGlobalContext();

    @Test
    public void testDatabaseManagerInstanceAndCreateTable(){
        try {
            DataBaseManager manager = DataBaseManager.getInstance(app);
            Assert.assertNotNull(manager);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void testDatabaseQueryAndInsert(){
        try {
            DataBaseManager manager = DataBaseManager.getInstance(app);
            if (manager != null) {
                ThreadInfo threadInfo = new ThreadInfo();
                threadInfo.setId(1);
                threadInfo.setTag("111");
                threadInfo.setUri("www.baidu.com");
                threadInfo.setStart(1);
                threadInfo.setEnd(100);
                threadInfo.setFinished(20);
                manager.insert(threadInfo);
            }
            List<ThreadInfo> threadInfoList = manager.getThreadInfos();
            String tag = threadInfoList.get(0).getTag();
            String url = threadInfoList.get(0).getUri();
            long start = threadInfoList.get(0).getStart();
            long finished = threadInfoList.get(0).getFinished();
            long end = threadInfoList.get(0).getEnd();
            int id = threadInfoList.get(0).getId();

            Assert.assertEquals(tag, "111");
            Assert.assertEquals(url, "www.baidu.com");
            Assert.assertEquals(id, 1);
            Assert.assertEquals(start, 1);
            Assert.assertEquals(finished, 20);
            Assert.assertEquals(end, 100);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void testDatabaseUpdate(){
        try {
            DataBaseManager manager = DataBaseManager.getInstance(app);
            if (manager != null) {
                ThreadInfo threadInfo = new ThreadInfo();
                threadInfo.setId(1);
                threadInfo.setTag("111");
                threadInfo.setUri("www.baidu.com");
                threadInfo.setStart(1);
                threadInfo.setEnd(100);
                threadInfo.setFinished(20);
                manager.insert(threadInfo);
            }
            //update
            manager.update("111",1,100);

            List<ThreadInfo> threadInfoList = manager.getThreadInfos();
            long finished = threadInfoList.get(0).getFinished();
            Assert.assertNotEquals(finished,20);//update before
            Assert.assertEquals(finished, 100);//update after
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Test
    public void testDatabaseDelete(){
        try {
            DataBaseManager manager = DataBaseManager.getInstance(app);
            if (manager != null) {
                ThreadInfo threadInfo = new ThreadInfo();
                threadInfo.setId(1);
                threadInfo.setTag("111");
                threadInfo.setUri("www.baidu.com");
                threadInfo.setStart(1);
                threadInfo.setEnd(100);
                threadInfo.setFinished(20);
                manager.insert(threadInfo);
            }
            //delete
            manager.delete("111");

            List<ThreadInfo> threadInfoList = manager.getThreadInfos();
            Assert.assertTrue(threadInfoList.size() == 0);

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
